<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-     Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 $sw    		= $_POST['sw'];
 $liv_nom    		= format_title($_POST['liv_nom']);
 $liv_auteur   		= format_author($_POST['liv_auteur']);
 $liv_coll		= $_POST['liv_coll'];
 $liv_genre_id		= $_POST['liv_genre_id'];
 $liv_etat_id		= $_POST['liv_etat_id'];
 $liv_location_id 	= $_POST['liv_location_id'];
 $liv_ed		= $_POST['liv_ed'];
 $liv_ISBN		= $_POST['liv_ISBN'];
 $liv_num		= $_POST['liv_num'];
 $resume		= $_POST['resume'];
 $motclefs		= $_POST['motclefs'];
 $temp			= $_POST['temp'];
 $noout			= $_POST['noout'];
 $liv_date		= $_POST['liv_date'];
 
if($sw==1)
{                       
   if($temp != 1)
   { $temp = 0;}
   if ($noout != 1) { $noout = 0;}
   $date_in = date("Y-m-d");
   $requete = "insert into livres (nom,auteur,collection,genre_ID,etat_ID,location_ID,edition,date_ed,ISBN,temp,noout,date_in,resume,motclefs,num) values (\"$liv_nom\",\"$liv_auteur\",\"$liv_coll\",\"$liv_genre_id\",\"$liv_etat_id\",\"$liv_location_id\",\"$liv_ed\",\"$liv_date\",\"$liv_ISBN\",$temp,$noout,\"$date_in\",\"$resume\",\"$motclefs\",\"$liv_num\")";
   if(!($resultat = biblio_db_query($requete)))
   { echo "PROBLEME :" .biblio_db_error();  echo "</BR> requete = ".$requete; exit;}   
   put_log(2,$requete,$user);                                                  
   $filename = biblio_db_insert_id() . ".jpg";
   $cover_path=get_coverspath();
   if ($_FILES['liv_cover']['tmp_name'])
   {
	system("cp ". $_FILES['liv_cover']['tmp_name'] ." $cover_path/$filename");
	system("cd $cover_path; mogrify -resize 200x200 $filename");
   }
   elseif ( preg_match ("/http\:\/\//i",$_POST['liv_url']))
   {
	system ("wget " . $_POST['liv_url'] ." -O $cover_path/$filename");
	system("cd $cover_path; mogrify -resize 200x200 $filename");
   }
   echo "<center><br><b>$liv_nom</b> ";
   echo _("est ajout&eacute; &agrave; la base de donn&eacute;es.");
   echo "<br>";
   echo "<br><a href=\"admin.php?action=add&type=livre\">";
   echo _("ajouter un autre livre");
   echo "</a><br></center>";
}
else
{
   $soap=enable_soap();
   if ($_POST['rechcode'] or !$soap)
   {			   
    if ($soap) $rechcode=return_isbn($_POST['rechcode']);

    $requete = "select id from locations"; 
    if(!($resultat = biblio_db_query($requete)))
    { echo "PROBLEME :" .biblio_db_error(); exit;}
    if(biblio_db_num_rows($resultat)>0)
    {
     #recherche du livre sur amazon 
     if ($soap) $o_livre=get_book_amazon_isbn($rechcode)->Details[0];
     ?>
        <center><H3>
	<?php echo _("Ajouter un livre"); ?>
	</H3>
        <form action=admin.php METHOD=POST>
        <table border=0>
        <tr>
        <td>
	<?php echo _("Titre"); ?>
	:</td><td><input type="text" name="liv_nom" size=30 
	value="<?php if ($soap) echo utf8_decode($o_livre->ProductName); ?>"></td></tr>
        <tr>
        <td>
	<?php echo _("Num&eacute;ro"); ?>
	:</td><td><input type="text" name="liv_num" size=4"></td></tr>
        <tr>
        <td>
	<?php echo _("Auteur"); ?>
	:</td><td><input type="text" name="liv_auteur" size=30
	value="<?php if ($soap) echo utf8_decode(implode(", ",$o_livre->Authors)); ?>"></td>
	</td>
        <tr>
        <td>
	<?php echo _("Collection"); ?>
	:</td><td><input type="text" name="liv_coll" size=30></td>
        <tr>
        <td>
	<?php echo _("Edition"); ?>
	:</td><td><input type="text" name="liv_ed" size=15
	value="<?php if ($soap) echo utf8_decode($o_livre->Manufacturer); ?>"></td>
        <tr>
        <td>
	<?php echo _("Date"); ?>
	:</td><td><input type="text" name="liv_date" size=10
	value="<?php if ($soap) echo substr($o_livre->ReleaseDate,-4); ?>"></td>
	</td>
        <tr>
        <td>
	<?php echo _("Genre"); ?>
	:</td><td><select NAME = "liv_genre_id">
<?php
	$genres = get_genres();
	if ($genres) {
		foreach ($genres as $key => $value) 
			print "<option VALUE=\"$key\">$value</option>";
	}
?>
        </select></td>
	<tr>
	<td>
	<?php echo _("R&eacute;sum&eacute;"); ?>
	:</td><td><TEXTAREA name="resume" cols="50" rows="5"></TEXTAREA></td>
	<tr>
	<td>
	<?php echo _("Mots cl&eacute;"); ?>
	:</td><td><input type="text" name="motclefs" size=30 maxlength=200>
	<small>(
	<?php echo _("s&eacute;par&eacute;s par un espacement"); ?>
	)</small></td>
        <tr>
        <td>
	<?php echo _("Etat"); ?>
	:</td><td><select NAME = "liv_etat_id">                                
<?php
	$etats =get_etats();
	if ($etats) {
		$i =count($etats);
		foreach ($etats as $id => $value)  {
			$i--;
			if ($i <= 0)
				print "<option VALUE=\"$id\" selected=\"selected\">$value</option>";
			else
				print "<option VALUE=\"$id\">$value</option>";
		}
	}
?>
        </select></td>
        <tr>
        <td>
	<?php echo _("Location"); ?>
	:</td><td><select NAME = "liv_location_id">
<?php
	$locations = get_locations();
	if ($locations) {
		foreach ($locations as $id => $location) {
			echo '<option VALUE="'.$id.'">';
			echo $location.'</option>';
		}
	}
?>
        </select></td>
        <tr>
        <td>
	<?php echo _("Temporaire"); ?>
	:</td><td><input type="checkbox" NAME = "temp" value=1></td>
        <tr>
        <td>
	<?php echo _("Ne peux sortir !"); ?>
	:</td><td><input type="checkbox" NAME = "noout" value=1></td>
        <tr>
        <td>
	<?php echo _("ISBN"); ?>
	:</td><td><input type="text" name="liv_ISBN" size=13
	value=<?php if ($soap) echo $rechcode; ?>></td>
	</tr>
	<tr>
	<td>
	<?php echo _("Couverture"); ?>
	:</td><td><input type=file name="liv_cover"><i>
	<?php echo _("fichier"); ?> 
	</i><br>
	<input type="text" size=30" name="liv_url"
	value="<?php if ($soap) echo $o_livre->ImageUrlLarge; ?>">
	<i>
	<?php echo _("ou url"); ?> 
	</i></td>
	</tr>
        </table><br>
        <input type="hidden" name="user" value="<?php echo $user; ?>">
        <input type="hidden" name="action" value="<?php echo $action; ?>">
        <input type="hidden" name="type" value="<?php echo $type; ?>">
        <input type="hidden" name="sw" value="1">
        <input type="Submit" value="
	<?php echo _("Enregistrer"); ?>
	">
        </form>
        </center>
        <?php
    }
    else {
     echo "<center><h3>";
     echo _("Vous devez encodez au moins un empacement avant d'encoder un nouveau livre !");
     echo "</center>";
    }
   }
   else
   {
	# entree de l'isbn pour recherche sur amazon ;-)
	?>
	<br><br>
	<center>
        <form action=admin.php method=POST>
	  <table id=tabsearch>
	  <tr><td><?php echo _("ISBN"); ?>&nbsp;:&nbsp;</td>
	      <td><input type=text name=rechcode size=15>
	  </tr>
	  </table>
          <input type="hidden" name="user" value="<?php echo $user; ?>">
          <input type="hidden" name="action" value="<?php echo $action; ?>">
          <input type="hidden" name="type" value="<?php echo $type; ?>">
	</form>
	</center>
	<?php
   }
}
?>              
<br><br>
<div>
<span id=left-text>
<a href="admin.php?menu=1">
<?php echo _("retour menu administration"); ?>
</a><br>
<a href="index.php">
<?php echo _("retour menu principal"); ?>
</a></span>
</div>
<br>
